/**
 **************************************************************************************
 * @file    drv_tmailbox.c
 * @brief   Driver for the mailbox between MCU and DSP
 *
 * @author  Aixing.Li
 * @version V2.0.0
 *
 * &copy; 2019 Mobvoi Corporation Ltd. All rights reserved.
 **************************************************************************************
 */

#ifndef __DRV_MAILBOX_H__
#define __DRV_MAILBOX_H__

#ifdef __cplusplus
extern "C"
{
#endif //__cplusplus

#include <stdint.h>

#define MAILBOX_CMD_REG_IDX_CMD 0
#define MAILBOX_CMD_REG_IDX_PARAM0 1
#define MAILBOX_CMD_REG_IDX_PARAM1 2
#define MAILBOX_CMD_REG_IDX_PARAM2 3

    /**
 * @brief MAILBOX COMMAND DESCRIPTION
 * ___________________________________________
 * | FLAGs  | Reserved | Group IDs | Cmd IDs |
 * -------------------------------------------
 */

#define MAILBOX_CMD_NEED_RSP_FLAG 0x80000000
#define MAILBOX_CMD_IS_RSP_FLAG 0x40000000
#define MAILBOX_CMD_FAST_RSP_FLAG 0x20000000
#define MAILBOX_CMD_FLAG_MASK 0xFF000000

    typedef enum _MAILBOX_CMD_SET
    {
        MAILBOX_CMD_COMMON_SUBSET = 0x0000,
        MAILBOX_CMD_REG_READ,
        MAILBOX_CMD_REG_WRITE,
        MAILBOX_CMD_CYCLE_COUNTER_RESET,
        MAILBOX_CMD_CYCLE_COUNTER_GET,
        MAILBOX_CMD_AUDIO_COMMON_SUBSET = 0x0100,
        MAILBOX_CMD_AUDIO_INIT,
        MAILBOX_CMD_AUDIO_DEINIT,
        MAILBOX_CMD_AUDIO_FIFO_CONFIG,
        MAILBOX_CMD_AUDIO_FIFO_STATUS,
        MAILBOX_CMD_AUDIO_ADC_SUBSET = 0x0200,
        MAILBOX_CMD_AUDIO_ADC_INIT,
        MAILBOX_CMD_AUDIO_ADC_DEINIT,
        MAILBOX_CMD_AUDIO_ADC_CONFIG,
        MAILBOX_CMD_AUDIO_ADC_SAMPLE_RATE_SET,
        MAILBOX_CMD_AUDIO_ADC_SAMPLE_RATE_GET,
        MAILBOX_CMD_AUDIO_ADC_ANALOG_VOLUME_SET,
        MAILBOX_CMD_AUDIO_ADC_ANALOG_VOLUME_GET,
        MAILBOX_CMD_AUDIO_ADC_DIGITAL_VOLUME_SET,
        MAILBOX_CMD_AUDIO_ADC_DIGITAL_VOLUME_GET,
        MAILBOX_CMD_AUDIO_ADC_VOLUME_SET,
        MAILBOX_CMD_AUDIO_ADC_VOLUME_GET,
        MAILBOX_CMD_AUDIO_ADC_ENABLE,
        MAILBOX_CMD_AUDIO_ADC_FILL_BUF_SIZE_CHECK,
        MAILBOX_CMD_AUDIO_ADC_BUF_READ,
        MAILBOX_CMD_AUDIO_DAC_DATA_FILL_BUF_SIZE_CHECK,
        MAILBOX_CMD_AUDIO_DAC_DATA_BUF_READ,
        MAILBOX_CMD_AUDIO_ANC_BUF_READ,
        MAILBOX_CMD_AUDIO_DAC_SUBSET = 0x0300,
        MAILBOX_CMD_AUDIO_DAC_INIT,
        MAILBOX_CMD_AUDIO_DAC_DEINIT,
        MAILBOX_CMD_AUDIO_DAC_CONFIG,
        MAILBOX_CMD_AUDIO_DAC_SAMPLE_RATE_SET,
        MAILBOX_CMD_AUDIO_DAC_SAMPLE_RATE_GET,
        MAILBOX_CMD_AUDIO_DAC_ANALOG_VOLUME_SET,
        MAILBOX_CMD_AUDIO_DAC_ANALOG_VOLUME_GET,
        MAILBOX_CMD_AUDIO_DAC_DIGITAL_VOLUME_SET,
        MAILBOX_CMD_AUDIO_DAC_DIGITAL_VOLUME_GET,
        MAILBOX_CMD_AUDIO_DAC_VOLUME_SET,
        MAILBOX_CMD_AUDIO_DAC_VOLUME_GET,
        MAILBOX_CMD_AUDIO_DAC_ENABLE,
        MAILBOX_CMD_AUDIO_DAC_FILL_BUF_SIZE_CHECK,
        MAILBOX_CMD_AUDIO_DAC_FREE_BUF_SIZE_CHECK,
        MAILBOX_CMD_AUDIO_DAC_BUF_WRITE,
        MAILBOX_CMD_ANC_SUBSET = 0x0400,
        MAILBOX_CMD_ANC_STATUS_SWITCH,
        MAILBOX_CMD_ANC_VOLUME_CHANGE,
        MAILBOX_CMD_ANC_ENABLE,
        MAILBOX_CMD_ANC_WHITE_NOISE_OUT,
        MAILBOX_CMD_ANC_PULSE_OUT,
        MAILBOX_CMD_ANC_RESERVED,
        MAILBOX_CMD_ANC_MIC3_SET,
        MAILBOX_CMD_ANC_MIC5_SET,
        MAILBOX_CMD_ANC_MIC2_SET,
        MAILBOX_CMD_ANC_MIC4_SET,
        MAILBOX_CMD_ANC_EC0_SET,
        MAILBOX_CMD_ANC_EC1_SET,
        MAILBOX_CMD_ANC_CTRL,
        MAILBOX_CMD_ANC_VOLUME0_CHANGE,
        MAILBOX_CMD_ANC_VOLUME1_CHANGE,
        MAILBOX_CMD_ANC_MIC_SELECT,

        MAILBOX_CMD_OTHER_SUBSET = 0x0500,
        MAILBOX_CMD_ANC_GAIN_SET,
        MAILBOX_CMD_ANC_DIG_GAIN_SET,
        MAILBOX_CMD_ANC_ANA_GAIN_SET,
        MAILBOX_CMD_SET_DSP_FUNC,
        MAILBOX_CMD_CVC_ENABLE,
        MAILBOX_CMD_REVERB_ENABLE,
        MAILBOX_CMD_MODE_CHANGE,
        MAILBOX_CMD_DAC_CHANGE,
        MAILBOX_CMD_USER_DSP_TUNING,
        MAILBOX_CMD_CVC_DUMP_DATA,
        MAILBOX_CMD_CVC_AGC_GAIN_SET,
        MAILBOX_CMD_SET_MIC_DATA_PATH,
        MAILBOX_CMD_AUDIO_ADC_OUT_GAIN_SET,
        MAILBOX_CMD_AUDIO_SET_MAIN_MIC,
        MAILBOX_CMD_ENC_UPDATE_GAIN,
        MAILBOX_CMD_USER_WK,
        MAILBOX_CMD_MCU_AWAKEN,
        MAILBOX_CMD_AUDIO_TWS_ROLE_STATE,
        MAILBOX_CMD_GET_KWS_RES,
		MAILBOX_CMD_SET_SNORE_THRESHOLD,
        MAILBOX_CMD_TEST_SUBSET = 0xFF00,
        MAILBOX_CMD_SET_MASK = 0xFFFF
    } MAILBOX_CMD_SET;

    typedef struct _MailBoxCmd
    {
        uint32_t cmd;
        uint32_t param0;
        uint32_t param1;
        uint32_t param2;
    } MailBoxCmd;

    void mailbox_mcu2dsp_send(uint32_t cmd, uint32_t param0, uint32_t param1, uint32_t param2);
    void mailbox_mcu2dsp_resp(uint32_t cmd, uint32_t param0, uint32_t param1, uint32_t param2);
    void mailbox_dsp2mcu_send(uint32_t cmd, uint32_t param0, uint32_t param1, uint32_t param2);
    void mailbox_dsp2mcu_resp(uint32_t cmd, uint32_t param0, uint32_t param1, uint32_t param2);

    void mailbox_mcu2dsp_send_until_recv(uint32_t cmd, uint32_t param0, uint32_t param1, uint32_t param2);
    void mailbox_dsp2mcu_send_until_recv(uint32_t cmd, uint32_t param0, uint32_t param1, uint32_t param2);

    uint32_t mailbox_mcu2dsp_is_busy(void);
    uint32_t mailbox_dsp2mcu_is_busy(void);
    uint32_t mailbox_mcu2dsp_ack_get(uint32_t idx);
    uint32_t mailbox_dsp2mcu_ack_get(uint32_t idx);

    void mailbox_mcu_isr(void);
    void mailbox_mcu_cmd_handler(MailBoxCmd *mbc);
    void mailbox_dsp_isr(void);
    void mailbox_dsp_cmd_handler(MailBoxCmd *mbc);

#ifdef __cplusplus
}
#endif //__cplusplus

#endif //__DRV_MAILBOX_H__
